package jgl.vajra.driver.widget.base;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;

import jgl.vajra.driver.R;
import jgl.vajra.driver.permissions.PermissionsManager;
import jgl.vajra.driver.widget.SwipeWindowHelper.SwipeWindowHelper;
import jgl.vajra.driver.widget.SystemBarTintManagerHelper;
import jgl.vajra.driver.widget.mvvm.ActivityLiftCycle;
import jgl.vajra.driver.widget.mvvm.view.AppActivityManager;
import jgl.vajra.driver.widget.push.JPushManager;

import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;

/**
 * Created by zsq on 2018/7/28.
 */

public abstract class BaseActivity extends AppCompatActivity implements ActivityLiftCycle {

    private static final String TAG = "XXSY";

    public static final int WRITE_REQUEST_CODE = 1001;
    public String[] permissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };

    //侧滑退出页面
    private SwipeWindowHelper mSwipeWindowHelper;
    protected boolean isFirstIn = true;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        Log.e(TAG, " ++++++++++++++++ class title = " + getClass().getSimpleName());
        if (getLayoutId() <= 0) {
            throw new NullPointerException("layout Id is null ");
        }
        initStateBar();
        //DensityManager.setDensityOrientation(this, isDensityOrientation() ? DensityManager.WIDTH : DensityManager.HEIGHT);
        AppActivityManager.getAppActivityManager().addActivity(this);
        // 禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }

    protected boolean isRegisterEvent() {
        return false;
    }

    protected void registerEvent() {
        if (isRegisterEvent() && !EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        isFirstIn = true;
    }

    @Override
    public void initEvent() {

    }

    /**
     * intro 状态栏设置
     */
    protected void initStateBar() {
        if (isNeedLoadStatusBar()) {
            SystemBarTintManagerHelper.getInsatance().initStateBar(this, getStateBarColor(), isTransparentStateBar(), isFitsSystemWindows());

        }
    }

    protected boolean isFitsSystemWindows() {
        return true;
    }

    protected boolean isNeedLoadStatusBar() {
        return true;
    }

    protected boolean isTransparentStateBar() {
        return false;
    }

    protected int getStateBarColor() {
        return R.color.white;
    }

    /**
     * intro activity按照什么方式适配 true 横向适配, false 纵向适配
     */
    protected boolean isDensityOrientation() {
        return true;
    }

    /**
     * 是否支持滑动返回
     *
     * @return
     */
    protected boolean isSupportSlideBack() {
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!isSupportSlideBack()) {
            return super.dispatchTouchEvent(ev);
        }

        if (mSwipeWindowHelper == null) {
            mSwipeWindowHelper = new SwipeWindowHelper(getWindow());
        }
        try {
            return mSwipeWindowHelper.processTouchEvent(ev) || super.dispatchTouchEvent(ev);
        } catch (Exception _e) {
            _e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isRegisterEvent() && EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
        AppActivityManager.getAppActivityManager().removeActivity(this);
        SystemBarTintManagerHelper.getInsatance().clearTintManager(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:// 点击返回图标事件
                onBackPressed();
                break;
            default:
                menuItemSelected(item);
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    public void menuItemSelected(MenuItem item) {

    }


    @TargetApi(23)
    public boolean isLacksOfPermission() {
        return ContextCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_DENIED
                || ContextCompat.checkSelfPermission(this, permissions[1]) == PackageManager.PERMISSION_DENIED;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case WRITE_REQUEST_CODE:
                if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                        && grantResults[1] == PackageManager.PERMISSION_GRANTED) { //Permission granted.
                    Log.d(TAG, " ++++++  Permission granted +++");
                } else { //Permission denied.
                    Log.d(TAG, " ++++++  Permission denied +++");
                }
                break;
            case 1://权限控制器械中写死的
                PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
                break;

        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 判断是否存在推送消息处理
        //  PushManager.getInstance().checkPush(this, this.getClass().getSimpleName());
        JPushManager.getInstance().checkJPush(this, this.getClass().getSimpleName());
        MobclickAgent.onResume(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
